package cn.com.oceansoft.modules.news.controller;

import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.base.entity.ApiQueryBase;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by chengy on 2016/11/3.
 */
@Controller
@RequestMapping(value = "news")
@Api(value = "警务公开", description = "警务公开后台")
public class Newscontroller {
    @Resource
    protected ConfigInfo configInfo;

    /**
     * 警务公开（首页）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "news_index", method = GET)
    public String gotorename(HttpServletRequest request, ModelMap model) {
        String type = request.getParameter("type");
        if (null == type || "".equals(type) || "null".equals(type)) {
            type = "jwzx";
        }
        model.addAttribute("type", type);
        return "news/new_index";
    }

    /**
     * 警务公开（列表）
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "newslist", method = {GET, POST})
    public JSONObject newslist(ApiQueryBase query, HttpServletRequest request) {
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        String title = request.getParameter("title").trim();
        String type = request.getParameter("type");
        String url = "";
        if (type.equals("gsgg")) {
            url = configInfo.getMsHostUrl() + "news/gsgg?ps=" + query.getPs() + "&pi=" + query.getPi() + "&kw=" + "";
        } else {
            url = configInfo.getMsHostUrl() + "news/page?ps=" + query.getPs() + "&pi=" + query.getPi() + "&kw=" + type;
        }
        if (!"".equals(title) && null != title && !"null".equals(title)) {
            url += "&q_title_s_lk=" + title;
        }
        if (!"".equals(start) && null != start && !"null".equals(start)) {
            url += "&q_public_time_d_ge=" + start;
        }
        if (!"".equals(end) && null != end && !"null".equals(end)) {
            url += "&q_public_time_d_le=" + end;
        }
        url+="&sb=public_time&sd=desc&q_deleted_s_eq=N";
        String result = HttpClientUtil.doGet(url);
        return JSONObject.parseObject(result);

    }

    /**
     * 获取详情
     *
     * @return
     */
    @RequestMapping(value = "newsdetail", method = {GET, POST})
    public String detail(@RequestParam("guid") String guid, @RequestParam("type") String type, HttpServletRequest request, Model model) {
        String result = "";
        if (type.equals("gsgg")) {
            result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "news/notices/" + guid);
        } else {
            result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "news/" + guid);
        }
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        model.addAttribute("list", apiResult.getData());

        model.addAttribute("type", type);
        return "news/new_detail";
    }


    /**
     * 黑龙江新闻中心首页
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "hlj-index", method = GET)
    public String gotoHljIndex(HttpServletRequest request, Model model) {
        String newsType = request.getParameter("newsType");
        if (StringUtils.isBlank(newsType)) {
            newsType = "zhyw";
        }
        model.addAttribute("newsType", newsType);
        return "news_hlj/new_index";
    }

    /**
     * 黑龙江新闻列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "hlj-list", method = {GET, POST})
    public JSONObject getHljList(ApiQueryBase query, HttpServletRequest request) {
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        String title = request.getParameter("title");
        String newsType = request.getParameter("newsType");
        String url = "";
        if (newsType.equals("zxgg")) {
            url = configInfo.getMsHostUrl() + "/news/gsgg?kw=" + newsType + "&ps=" + query.getPs() + "&pi=" + query.getPi();
        } else {
            url = configInfo.getMsHostUrl() + "/news/page?kw=" + newsType + "&ps=" + query.getPs() + "&pi=" + query.getPi();
        }
        if (!"".equals(start) && null != start && !"null".equals(start)) {
            url += "&q_public_time_d_ge=" + start;
        }
        if (!"".equals(end) && null != end && !"null".equals(end)) {
            url += "&q_public_time_d_le=" + end;
        }
        if (!"".equals(title) && null != title && !"null".equals(title)) {
            url += "&q_title_s_lk=" + title;
        }
        String result = HttpClientUtil.doGet(url);
        return JSONObject.parseObject(result);

    }

    /**
     * 获取黑龙江新闻详情
     *
     * @return
     */
    @RequestMapping(value = "hlj-detail", method = {GET, POST})
    public String getHljDetail(@RequestParam("guid") String guid, @RequestParam("newsType") String newsType, HttpServletRequest request, Model model) {
        String result = "";
        if (newsType.equals("zxgg")) {
            result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/notices/" + guid);
        } else {
            result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/" + guid);
        }
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        model.addAttribute("newDetail", apiResult);
        model.addAttribute("newsType", newsType);
        return "news_hlj/new_detail";
    }
}
